import { HttpException, HttpStatus, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { Account } from './account.entity';
import { CreateDto } from './dto/create.dto'
import { RemoveDto } from './dto/remove.dto'
import { UpdateDto } from './dto/update.dto'
import { GetAccountsDto, accountResponse } from './dto/get-account.dto'

@Injectable()
export class AccountService {
  constructor(
    @InjectRepository(Account)
    private accountRepository: Repository<Account>,
  ) { }

  async createAccount(createDto: CreateDto): Promise<{ isCreated: boolean, message: string, code: number }> {

    const newAccount = new Account();
    newAccount.userId = createDto.userId;
    newAccount.amount = createDto.amount;
    newAccount.accountIcon = createDto.accountIcon;
    newAccount.accountName = createDto.accountName;
    newAccount.attach = createDto.attach;
    newAccount.note = createDto.note;

    await this.accountRepository.save(newAccount);



    return { isCreated: true, message: "添加账户成功!", code: 200 };
  }

  async getAccountsByUserId(getAccountsDto: GetAccountsDto): Promise<{ data: Array<accountResponse>, message: string, code: number }> {
    if (!getAccountsDto.userId) {
      throw new HttpException('用户id不能为空!', HttpStatus.BAD_REQUEST);
    }
    const query = await this.accountRepository.findAndCount({ where: { userId: getAccountsDto.userId, status: "1" } });
    let message = query[1] ? "查询成功!" : "根据用户id,查询结果为空!";
    const data = query[0].map(v => ({
      dataId: v.dataId,
      userId: v.userId,
      createdDate: v.createdDate,
      accountIcon: v.accountIcon,
      accountName: v.accountName,
      amount: v.amount,
      attach: v.attach,
      note: v.note
    }));

    return { data, message, code: 200 };
  }

  async removeAccountsByDataIds(removeDto: RemoveDto): Promise<{ isRemoved: boolean, message: string, code: number }> {
    if (!removeDto.dataIds?.length) {
      throw new HttpException('dataIds不能为空!', HttpStatus.BAD_REQUEST);
    }
    const removed = await this.accountRepository.update({ dataId: In(removeDto.dataIds), status: "1" }, { status: "0" });
    if (removed.affected && removed.affected > 0) {
      return { isRemoved: true, message: removed.affected + "条记录, 删除成功!", code: 200 };
    } else {
      return { isRemoved: false, message: "删除失败!", code: 400 };
    }

  }

  async updateAccountByDataId(updateDto: UpdateDto): Promise<{ isUpdated: boolean, message: string, code: number }> {
    if (!updateDto.dataId) {
      throw new HttpException('dataId不能为空!', HttpStatus.BAD_REQUEST);
    }
    const updated = await this.accountRepository.update({ dataId: updateDto.dataId, status: "1" }, {
      amount: updateDto.amount,
      accountIcon: updateDto.accountIcon,
      accountName: updateDto.accountName,
      attach: updateDto.attach,
      note: updateDto.note
    });
    if (updated.affected && updated.affected > 0) {
      return { isUpdated: true, message: "修改成功!", code: 200 };
    } else {
      return { isUpdated: false, message: "修改失败!", code: 400 };
    }

  }
}